package com.example.sunmiv1.adapter;

import android.content.Context;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.StoreRetreatOrderDetailsBean;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PurchaseRefundDetailsAdapter extends RecyclerView.Adapter<PurchaseRefundDetailsAdapter.prViewHolder> {


    private Context mContext;
    private List<StoreRetreatOrderDetailsBean.DataBean> mList;
    private String pricePermission;// 門店查看價格權限

    public PurchaseRefundDetailsAdapter(Context context, List<StoreRetreatOrderDetailsBean.DataBean> list) {
        super();
        mContext = context;
        mList = list;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");
    }

    @Override
    public prViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_purchase_refund_detail, parent, false);
        return new prViewHolder(view);
    }

    @Override
    public void onBindViewHolder(prViewHolder holder, int position) {
        holder.infoPurOrder.setText(mList.get(position).getPurchase_order_number());
        holder.infoGoodsname.setText(mList.get(position).getGoods_name());
        Tools.hideViewInItem(mList.get(position).getGoods_code(), holder.goodsCode);
        holder.goodsNumber.setText(mList.get(position).getGoods_number());
        Tools.hideViewInItem(mList.get(position).getCompany(), holder.goodsUnit);
        holder.infoRetreatNum.setText(String.valueOf(mList.get(position).getRetreat_num()));
        holder.infoPurchasedPrice.setText(String.valueOf(mList.get(position).getPurchase_price()));
        holder.infoRefund.setText(String.valueOf(mList.get(position).getRetreat_price()));
        holder.infoDifferenceMoney.setText(String.valueOf(mList.get(position).getDifference_money()));

        Tools.hideViewInItem(mList.get(position).getRemarks(), holder.infoRemark);
        Tools.hideViewInItem(mList.get(position).getSku_name(), holder.infoSkuName);

        holder.goodsBuildDate.setText(mList.get(position).getBuild_date());
        holder.goodsQualityGuaranteePeriod.setText(mList.get(position).getQuality_guarantee_period());

        if ("2".equals(pricePermission)) {
            ((View) holder.infoPurchasedPrice.getParent()).setVisibility(View.GONE);
            ((View) holder.infoRefund.getParent()).setVisibility(View.GONE);
            ((View) holder.infoDifferenceMoney.getParent()).setVisibility(View.GONE);
        }
        if("1".equals(mList.get(position).getSerial_number_state())){
            holder.infoRetreatNum.setTextColor(ContextCompat.getColor(mContext,R.color.blue_3293FA));
        }else{
            holder.infoRetreatNum.setTextColor(ContextCompat.getColor(mContext,R.color.text_666));
        }
        final int index = position;

        holder.infoRetreatNum.setOnClickListener(v -> {
            if ("1".equals(mList.get(position).getSerial_number_state())) {
                Intent intent = new Intent(mContext, StringCodeOperationActivity.class);
                intent.putExtra("operation", 13);
                intent.putExtra("id", mList.get(index).getId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    static class prViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.info_pur_order)
        TextView infoPurOrder;
        @BindView(R.id.info_goodsname)
        TextView infoGoodsname;
        @BindView(R.id.info_sku_name)
        TextView infoSkuName;
        @BindView(R.id.group_sku_name)
        RelativeLayout groupSkuName;
        @BindView(R.id.goods_code)
        TextView goodsCode;
        @BindView(R.id.layout_goods_code)
        RelativeLayout layoutGoodsCode;
        @BindView(R.id.goods_number)
        TextView goodsNumber;
        @BindView(R.id.layout_goods_number)
        RelativeLayout layoutGoodsNumber;
        @BindView(R.id.goods_unit)
        TextView goodsUnit;
        @BindView(R.id.layout_unit)
        RelativeLayout layoutUnit;
        @BindView(R.id.info_retreat_num)
        TextView infoRetreatNum;
        @BindView(R.id.info_purchased_price)
        TextView infoPurchasedPrice;
        @BindView(R.id.info_refund)
        TextView infoRefund;
        @BindView(R.id.info_difference_money)
        TextView infoDifferenceMoney;
        @BindView(R.id.info_remark)
        TextView infoRemark;
        @BindView(R.id.group_remark)
        RelativeLayout groupRemark;
        @BindView(R.id.goods_build_date)
        TextView goodsBuildDate;
        @BindView(R.id.goods_quality_guarantee_period)
        TextView goodsQualityGuaranteePeriod;

        public prViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
